package com.fzsf.drawingboard.adapter;

import android.bluetooth.BluetoothDevice;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fzsf.drawingboard.utils.StringUtils;
import com.fzsf.drawingboard.R;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/8/3.
 */

public class BlueDeviceAdapter extends RecyclerView.Adapter<BlueDeviceAdapter.MyViewHolder> {

    private Context context;
    private List<BluetoothDevice> list;
    private final HashMap<String, BluetoothDevice> rssiMap = new HashMap<String, BluetoothDevice>();
    private OnItemDeviceClickListener listener;

    public interface OnItemDeviceClickListener {
        void itemDeviceClick(BluetoothDevice device);
    }

    public void setOnItemDeviceClickListener(OnItemDeviceClickListener onItemDeviceClickListener) {
        this.listener = onItemDeviceClickListener;
    }

    public BlueDeviceAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_blue_device, null);
        view.setLayoutParams(new RecyclerView.LayoutParams(-1, -2));
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        BluetoothDevice device = list.get(position);
        if ((device.getName() != null && device.getName().length() > 0)) {
            holder.tv_blue_device_name.setText(device.getName());
        } else {
            holder.tv_blue_device_name.setText("未知设备");
        }
        holder.tv_blue_device_name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.itemDeviceClick(list.get(position));
            }
        });
    }

    /**
     * 添加蓝牙设备
     */
    public void addDevice(BluetoothDevice device, int rssi) {
        if (null == device) {
            return;
        }
        String addr = device.getAddress();
        if (StringUtils.isNotEmpty(addr)) {
            if (!rssiMap.containsKey(addr)) {
                rssiMap.put(addr, device);
                list.add(device);
            }
        } else {
            list.add(device);
        }
        notifyItemChanged(list.size());
    }

    /**
     * 清楚当前所有展示的设备
     */
    public void clearAllDevices() {
        if (null != list) {
            list.clear();
            notifyDataSetChanged();
        }
    }

    public BluetoothDevice getDevice(int position) {
        return list.get(position);
    }

    public void clear() {
        list.clear();
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView tv_blue_device_name;
        private RelativeLayout rl_item_blue_device;

        public MyViewHolder(View itemView) {
            super(itemView);
            tv_blue_device_name = (TextView) itemView.findViewById(R.id.tv_blue_device_name);
            rl_item_blue_device = (RelativeLayout) itemView.findViewById(R.id.rl_item_blue_device);
        }
    }

}
